#!/usr/bin/env bash

source $HOME/.colors &>/dev/null

function translate() {
  typing=$(mktemp)
  rm -rf $typing && nano $typing
  msg=$(trans -b :en -no-auto -i $typing)
  typing=$(cat $typing)
  echo "${BOL_RED}Message:${BOL_YEL}${msg}${END}"
}

function gitpush() {
  pwd=$(pwd | cut -c-27)
  if [[ $pwd = "/run/media/kleidione/Source" ]]; then
    echo "${BOL_RED}No push!${END}"
  else
    if [[ ${1} = amend ]]; then
      echo "${BOL_YEL}Pushing with --force!${END}"
      git push -f
    else
      echo "${BOL_YEL}Pushing!${END}"
      git push
    fi
  fi
}

function cm() {
  translate
  if [[ ${1} ]]; then
    git commit --author "${1}" --date "$(date)" && gitpush
  else
    git commit --message $msg --signoff --author "kleidione Freitas <kleidione@gmail.com>" --date "$(date)" && gitpush
  fi
}

function amend() {
  if [[ ${1} ]]; then
    git commit --author "${1}" --signoff --amend --date "$(date)" && gitpush amend
  else
    git commit --signoff --amend --date "$(date)" && gitpush amend
  fi
}

function push() {
  REPO=$(pwd | sed "s/\/run\/media\/kleidione\/Source\/KRAKEN\///; s/\//_/g")
  echo $REPO
  GITHOST=github
  ORG=AOSPK-WIP
  BRANCH=eleven
  FORCE=${1}
  TOPIC=${2}

  if [[ $REPO = "vendor_gapps" || $REPO = "vendor_google_gms" ]]; then
    GITHOST=gitlab
    ORG=AOSPK
  fi
  if [[ $REPO = "www" ]]; then
    ORG=AOSPK
    BRANCH=master
  fi
  if [[ $REPO = "build_make" ]]; then
    REPO=build
  fi
  if [[ $REPO = "packages_apps_PermissionController" ]]; then
    REPO=packages_apps_PackageInstaller
  fi
  if [[ $REPO = "vendor_qcom_opensource_commonsys-intf_bluetooth" ]]; then
    REPO=vendor_qcom_opensource_bluetooth-commonsys-intf
  fi
  if [[ $REPO = "vendor_qcom_opensource_commonsys-intf_display" ]]; then
    REPO=vendor_qcom_opensource_display-commonsys-intf
  fi
  if [[ $REPO = "vendor_qcom_opensource_commonsys_bluetooth_ext" ]]; then
    REPO=vendor_qcom_opensource_bluetooth_ext
  fi
  if [[ $REPO = "vendor_qcom_opensource_commonsys_packages_apps_Bluetooth" ]]; then
    REPO=vendor_qcom_opensource_packages_apps_Bluetooth
  fi
  if [[ $REPO = "vendor_qcom_opensource_commonsys_system_bt" ]]; then
    REPO=vendor_qcom_opensource_system_bt
  fi

  if [[ ${1} = "gerrit" ]]; then
    echo "${BOL_BLU}Pushing to gerrit.aospk.org/${GRE}${ORG}${END}/${BLU}${REPO}${END} - ${BRANCH} ${RED}${FORCE}${END}"
    if [ -z "${TOPIC}" ]
    then
      git push ssh://kleidione@gerrit.aospk.org:29418/${REPO} HEAD:refs/for/${BRANCH}
    else
      git push ssh://kleidione@gerrit.aospk.org:29418/${REPO} HEAD:refs/for/${BRANCH}%topic=${TOPIC}
    fi
  else
    echo "${BOL_BLU}Pushing to ${GITHOST}.com/${GRE}${ORG}${END}/${BLU}${REPO}${END} - ${BRANCH} ${RED}${FORCE}${END}"
    git push ssh://git@${GITHOST}.com/${ORG}/${REPO} HEAD:refs/heads/${BRANCH} ${FORCE}
  fi
}

